package com.example.shiwuyouproject.ui.fragment.my

import android.os.Bundle
import android.view.LayoutInflater
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityMycollectionBinding
import com.example.shiwuyouproject.ui.fragment.home.activity.CourseDetailsActivity
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.MyApplicationAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.bean.MyApplicationBean
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.MyCollectionViewModel
import com.example.shiwuyouproject.utils.ActStartUtils
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * 我的收藏
 */
class MyCollectionActivity:BaseVmActivity<ActivityMycollectionBinding, MyCollectionViewModel>() {
    override fun viewModelClass(): Class<MyCollectionViewModel>  = MyCollectionViewModel::class.java

    override fun getViewBinding(): ActivityMycollectionBinding {
        return  ActivityMycollectionBinding.inflate(layoutInflater)
    }

    override fun initView() {
        mBinding.myTitleBar.tvTitle.text = "我的收藏"

    }
    private var myApplicationAdapter: MyApplicationAdapter?=null
    override fun initData() {
        myApplicationAdapter=MyApplicationAdapter()
        mBinding.myRecyclerView.adapter=myApplicationAdapter
        val mview = LayoutInflater.from(this).inflate(R.layout.view_nodata, null)
        myApplicationAdapter?.emptyView = mview
        mBinding.mySmartRefreshLayout.autoRefresh()
    }
    private var pageNumber=1
    override fun setListener() {
        mBinding.myTitleBar.tvBack.setOnClickListener { finish() }
        mBinding.mySmartRefreshLayout.setOnRefreshLoadMoreListener(object :
            OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNumber=1
                mViewModel.getCommissionLists(pageNumber.toString())
            }
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNumber++
                mViewModel.getCommissionLists(pageNumber.toString())
            }
        })
        myApplicationAdapter?.setOnItemClickListener { adapter, view, position ->
            val item: MyApplicationBean? =myApplicationAdapter?.getItem(position)
            val mBundle = Bundle()
            mBundle.putString("post_id", item?.id)
            ActStartUtils.startAct(this, CourseDetailsActivity::class.java, mBundle)
        }
    }
    override fun observe() {
        mViewModel.getGenRenStatus.observe(this,{
            if(pageNumber==1) {
                myApplicationAdapter?.setNewData(it)
                mBinding.mySmartRefreshLayout.finishRefresh()
            }  else{
                myApplicationAdapter?.addData(it)
                mBinding.mySmartRefreshLayout.finishLoadMore()
            }
            if(pageNumber>1&&it.size==0){
                mBinding.mySmartRefreshLayout.finishLoadMoreWithNoMoreData()
            }
        })

    }
}